package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private SpuService spuService;

    @Autowired
    private SpuDetailService spuDetailService;

    @Autowired
    private SkuService skuService;

    /**
     * 根据id查询商品
     * @param id 商品id
     * @return 商品信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable Long id) {

        return ResponseEntity.ok(spuService.queryGoodsById(id));

    }

    /**
     * 分页查询spu
     * @param currentPage 当前页
     * @param pageSize 每页大小
     * @param saleable 是否为上架或下架商品
     * @param categoryId 分类id
     * @param brandId 品牌id
     * @param id spu的id
     * @return 当前页商品数据信息
     *
     *
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(

            @RequestParam(value = "page",defaultValue = "1") Integer currentPage,
            @RequestParam(value = "rows",defaultValue = "5") Integer pageSize,
            @RequestParam(value = "saleable",required = false) Boolean saleable,
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "brandId",required = false) Long brandId,
            @RequestParam(value = "id",required = false) Long id

    ){

        return ResponseEntity.ok(spuService.querySpuByPage(currentPage,pageSize,saleable, categoryId, brandId, id));

    }

    /**
     * 新增商品 consumes: 接收的数据类型 produces：返回的数据类型  默认就是这个可以不写
     * @param spuDTO 页面提交商品信息
     * @return
     */
    @PostMapping(value = "/spu",consumes = "application/json",produces = "application/json")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){

        spuService.saveGoods(spuDTO);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    /**
     * 修改商品上下架信息
     * @param id 商品spu的id
     * @param saleable true 上架  false 下架
     * @return 无
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSpuSaleable(

            @RequestParam("id") Long id,
            @RequestParam("saleable") Boolean saleable

    ) {

        spuService.updateSpuSaleable(id,saleable);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    /**
     * 修改商品信息
     * @param spuDTO 页面提交商品信息
     * @return 无
     */
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {

        spuService.updateGoods(spuDTO);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    /**
     * 根据id查询商品spu 单表查询
     * @param id 商品id
     * @return 商品信息
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id) {

        return ResponseEntity.ok(spuService.querySpuById(id));

    }

    /**
     * 根据spuId查询商品spuDetails详情
     * @param spuId 商品id
     * @return 商品的Detail
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long spuId) {

        return ResponseEntity.ok(spuDetailService.querySpuDetailById(spuId));

    }

    /**
     * 根据spuId查询商品sku集合详情
     * @param spuId 商品id
     * @return sku集合的数据信息
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long spuId) {

        return ResponseEntity.ok(skuService.querySkuBySpuId(spuId));

    }

    /**
     * 根据id集合查询sku集合
     * @param ids id的集合
     * @return sku的集合
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuIds(@RequestParam("ids") List<Long> ids) {

        return  ResponseEntity.ok(skuService.querySkuBySpuIds(ids));

    }

    /**
     * 根据spuId查询所有的规格参数值
     * @param spuId spu的id
     * @param searching 搜索条件字段
     * @return 规格参数值
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecsValues(

            @RequestParam("id") Long spuId,
            @RequestParam(value = "searching",required = false) Boolean searching

    ){

        return ResponseEntity.ok(this.spuDetailService.querySpecsValues(spuId,searching));

    }

    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(

            @RequestBody Map<Long,Integer> cartMap

            ){

        this.skuService.deductStock(cartMap);

        return ResponseEntity.noContent().build();

    }

}
